﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpPcap;
using PacketDotNet;
using TinyIM2013.Packets;
using System.Windows.Forms;
using System.Net;

namespace TinyIM2013
{
    public class TinyIMPacket
    {
        internal static void ProcessUdpPacket(TinyPacket tinyPacket)
        {
            byte[] packetData = tinyPacket.PayloadData;
            PCmd command = PacketUtility.GetPCmd(packetData);
            if (command == PCmd.CMD_0825)
            {
                if (packetData.Length == PacketUtility.P_SEND_0825_LENGTH)
                {
                    PSend p = new PSend_0825();
                    if (p.TryParse(packetData, out p))
                    {
                        IMKeyCollection.Instance.SetKey(p.PacketHead.IMAccount, PCmd.CMD_0825, ((PSend_0825)p).GetTEAKey(packetData));
                        PacketSeqCollection.Instance.SetSequenceWithCommand(PCmd.CMD_0825, p.PacketHead.Sequence);
                    }
                }
                else if (packetData.Length == PacketUtility.P_RECEIVE_0825_LENGTH)
                {
                    PReceive_0825 p = new PReceive_0825();
                    if (p.TryParse(packetData, out p))
                    {
                        if (p.TokenData != null)
                        {
                            TokenDataCollection.Instance.SetTokenDataWithCommandName(PCmd.CMD_0825, p.TokenData);
                        }
                    }
                }
                else if (packetData.Length == PacketUtility.P_RECEIVE_0825_REDIRECT_LENGTH)
                {
                    PReceive_0825_Redirect p = new PReceive_0825_Redirect();
                    if (p.TryParse(packetData, out p))
                    {
                        if (p.TokenData != null)
                        {
                            TokenDataCollection.Instance.SetTokenDataWithCommandName(PCmd.CMD_0825, p.TokenData);
                        }
                        if (p.ServerForRedirect != null)
                        {
                            IPAddress ip = new IPAddress(p.ServerForRedirect);
                            TinyPacket.AddServerToServerDomainList(ip.ToString(), IPProtocolType.UDP);
                        }
                    }
                }
            }
            else if (command == PCmd.CMD_0826)
            {
                PSend p = new PSend_0826();
                if (p.TryParse(packetData, out p))
                {
                    IMKeyCollection.Instance.SetKey(p.PacketHead.IMAccount, PCmd.CMD_0825, ((PSend_0826)p).GetTEAKey(packetData));
                    PacketSeqCollection.Instance.SetSequenceWithCommand(PCmd.CMD_0825, p.PacketHead.Sequence);
                }
            }
        }

        public static string ByteArrayToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }

        internal static void ProcessTcpPacket(TinyPacket tinyPacket)
        {
            throw new NotImplementedException();
        }
    }
}
